﻿Imports System.IO
Imports System.Data.SqlClient

Public Class Libreria

    Dim base_d_datos As String = "1"
    Private url_Servidor As String
    Private enlace As ADODB.Connection
    ' /**
    ' * 
    ' */
    Public Sub Acceder_Conf()
        Try
            Me.base_d_datos = Configuracion.Default.BASE_DATOS.ToString()
            Console.WriteLine(" bd " & Me.base_d_datos)
        Catch ex As IOException
            Console.WriteLine("No existe fichero se creara uno")
        End Try
    End Sub

    Sub cargar_conf()
        If Configuracion.Default.CONTROLADOR.Equals(0) Then
            cargar_SQL()
            Console.WriteLine(" cfg SQL")
        ElseIf Configuracion.Default.CONTROLADOR.Equals(1) Then
            cargar_MYSQL()
            Console.WriteLine(" cfg MYSQL")
        ElseIf Configuracion.Default.CONTROLADOR.Equals(2) Then
            cargar_ACCESS()
            Console.WriteLine(" cfg ACCESS")
        End If

    End Sub
    Sub cargar_SQL()
        Dim url_Servidor As String
        url_Servidor = "Provider=sqloledb;Data Source=localhost\SQLEXPRESS;Initial Catalog=" & base_d_datos & ";Integrated Security = True"
        Me.setUrl_Servidor(url_Servidor)

    End Sub
    Sub cargar_MYSQL()
        Dim url_Servidor As String
        url_Servidor = "Provider=MySQLProv;" & "Data Source=localhost\" & base_d_datos & ";" & _
          "User Id=root;Password=123456789"

        Me.setUrl_Servidor(url_Servidor)

    End Sub
    Sub Setdriver_sql()
        Try
            Dim cnn As ADODB.Connection
            cnn = New ADODB.Connection
            cnn.Open(getUrl_Servidor())
            setEnlace(cnn)
        Catch ex As SqlException
            Console.WriteLine(ex.Message)
        End Try

    End Sub
    Sub Setdriver_mysql()
        Try
            Dim cnn As ADODB.Connection
            cnn = New ADODB.Connection
            cnn.Open(getUrl_Servidor())
            setEnlace(cnn)
        Catch ex As SqlException
            Console.WriteLine(ex.Message)
        End Try

    End Sub
    Sub Setdriver_access()
        Try
            Dim enlaces As ADODB.Connection
            enlaces = New ADODB.Connection
            enlaces.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & url_Servidor & ";"
            enlaces.Open()
            setEnlace(enlaces)
        Catch ex As SqlException
            Console.WriteLine(ex.Message)
        End Try
    End Sub


    Sub cargar_ACCESS()
        Try
            Dim url_Servidor1 As String
            'es System.IO.Directory.GetCurrentDirectory.ToString o curDir()
            url_Servidor1 = System.IO.Directory.GetCurrentDirectory.ToString & "\" & base_d_datos & ".mdb"
            Console.WriteLine(url_Servidor1)
            setUrl_Servidor(url_Servidor1)
        Catch ex As IOException
            Console.WriteLine(ex.Message)
        End Try

    End Sub
    ' /**
    ' * @return the url_Servidor
    ' */
    Public Function getUrl_Servidor() As String
        Return url_Servidor

    End Function

    ' /**
    ' * @param url_Servidor the url_Servidor to set
    ' */
    Public Sub setUrl_Servidor(ByVal url_Servidor As String)
        Me.url_Servidor = url_Servidor

    End Sub

    ' /**
    ' * @return the enlace
    ' */
    Public Function getEnlace() As ADODB.Connection
        Return enlace

    End Function

    ' /**
    ' * @param enlace the enlace to set
    ' */
    Public Sub setEnlace(ByVal enlace As ADODB.Connection)
        Me.enlace = enlace

    End Sub

End Class
